package cn.sxt.config;

import cn.xbtop.service.UserService;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import org.springframework.context.annotation.Bean;


//@Configuration
public class DubboConfig {

    /**
     * 声明应用名
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig("ego-boot-user-service-provider");
        return config;
    }

    /**
     * 声明注册中心配置
     */
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig config = new RegistryConfig("zookeeper://103.40.26.92:2181");
        return config;
    }

    /**
     * 声明暴露协议和端口
     */
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig config = new ProtocolConfig("dubbo", 20880);
        return config;
    }

    /**
     * 暴露UserServiceImpl的服务
     */
    @Bean
    public ServiceConfig<UserService> serviceConfigUserService(UserService userService
            ,ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
        ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(userService);

        // 注入applicationName
        serviceConfig.setApplication(applicationConfig);
        // 设置注册中心
        serviceConfig.setRegistry(registryConfig);

        // 暴露的方法
        serviceConfig.export();
        return serviceConfig;
    }

}
